/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO.Comentario;

import Model.ComentarioDialog;
import DAO.Usuario.HibernateUtil;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.hibernate.Session;

/**
 *
 * @author pedro
 */
@Path("/comentario")
public class ComentarioUsuario {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getComentario() {
        Session session = null;
        String resultado;
        try {
            session = HibernateUtil.getSession();
            HibernateUtil.beginTransaction();
            ArrayList<ComentarioDialog> lista = (ArrayList<ComentarioDialog>) session.createQuery("From Comentarios").list();

            HibernateUtil.commitTransaction();
            HibernateUtil.closeSession();

            Gson g = new Gson();
            resultado = g.toJson(lista);
        } catch (Exception e) {
            System.out.println(e);
            resultado = "Error";
            session.getTransaction().rollback();
        } finally {
            try {
                HibernateUtil.closeSession();
            } catch (Throwable e) {
                System.out.println(e.getMessage());
            }
        }
        return resultado;
    }

    @POST
    @Consumes(MediaType.TEXT_PLAIN)
    public Response getComentarios(String cod) {
        String resultado = "";
        ArrayList<ComentarioDialog> comments = new ArrayList();
        Gson g = new Gson();
        Session session = null ;
        HibernateUtil.beginTransaction();

        try {
            session= HibernateUtil.getSession();
            List<Object> lista = session.createSQLQuery("SELECT nome,comentario,cod_usuario,comentarios.cod FROM user,comentarios  WHERE cod_post=? AND  user.cod=comentarios.cod_usuario")
                    .setInteger(0, Integer.parseInt(cod))
                    .list();
            HibernateUtil.commitTransaction();
            HibernateUtil.closeSession();

            for (int i = 0; i < lista.size(); i++) {
                ComentarioDialog comment = new ComentarioDialog();
                Object[] row = (Object[]) lista.get(i);
                String aux = (String) row[0];
                comment.setUsername((String) row[0]);
                comment.setComentario((String) row[1]);
                comment.setCod((Integer) row[2]);
                comment.setCodComentario((Integer) row[3]);
                comments.add(comment);
            }
            resultado = g.toJson(comments);

        } catch (Exception e) {
            System.out.println(e);
            resultado = "Error";
            session.getTransaction().rollback();
        }
        finally {
            try {
                HibernateUtil.closeSession();
            } catch (Throwable e) {
                System.out.println(e.getMessage());
            }
        }
        return Response.status(201).entity(resultado).build();
    }
}
